package com.tv.enoguia;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.tv.enoguia.bean.Celler;
import com.tv.enoguia.database.MySQLiteHelper;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailBussinessActivity extends Activity implements
		OnTouchListener {

	TextView title;
	ImageButton btnBack, btnFace, btnTw;
	
	Button btnPhone, btnWeb, btnEmail;
	
	Dialog dC, dW, dM;
	
	TextView titleDirection;
	ViewGroup llDirection;
	
	ImageButton btnLike;
	
	String id;
	MySQLiteHelper db;
	
	int liked;
	
	Celler celler = new Celler();
	TextView titleCategory, txtDientich, txtSanluong, txtDes, txtAddress, txtListVins; 
	ImageView imgLogo, imgCover;
	DisplayImageOptions options = new DisplayImageOptions.Builder()
								.cacheInMemory(false).cacheOnDisc(true)
								.bitmapConfig(Config.RGB_565)
								.build();
	
	
	TextView txtSchedule, txtPrice, txtLang, txtCapicity, txtWine, txtVisit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_bussiness);

		id = getIntent().getStringExtra("id");
		
		db = new MySQLiteHelper(this);
		
		
		
		liked = getIntent().getIntExtra("liked", 0);
		
		if (liked == 1){
			celler = db.getDetailCellerLike(id);
		} else {
			celler = db.getDetailCeller(id);	
		}
		
		
		
		
		title = (TextView) findViewById(R.id.title);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnBack.setVisibility(View.VISIBLE);
		
		btnPhone = (Button) findViewById(R.id.btn_phone);
		btnWeb = (Button) findViewById(R.id.btn_web);
		btnEmail = (Button) findViewById(R.id.btn_email);
		
		titleDirection = (TextView) findViewById(R.id.txt_title_direction);
		titleCategory = (TextView) findViewById(R.id.txt_name_category);
		txtDientich = (TextView) findViewById(R.id.txt_dientich);
		txtSanluong = (TextView) findViewById(R.id.txt_sanluong);
		txtDes = (TextView) findViewById(R.id.txt_des);
		
		imgLogo = (ImageView) findViewById(R.id.img_avatar);
		imgCover = (ImageView) findViewById(R.id.img_bussiness);
		txtAddress = (TextView) findViewById(R.id.txt_address);
		txtListVins = (TextView) findViewById(R.id.lista_vins);
		
		llDirection = (ViewGroup) findViewById(R.id.ll_direction);
		
		btnLike = (ImageButton) findViewById(R.id.ll_favor);
		
		Log.i("DATA", "LIKE: "+liked);
		
		if (liked == 1){
			btnLike.setImageResource(R.drawable.btn_like_selector_dis);
		} else {
			btnLike.setImageResource(R.drawable.btn_like_selector);	
		}
		
		btnFace = (ImageButton) findViewById(R.id.btn_facebook);
		btnTw = (ImageButton) findViewById(R.id.btn_tw);
		
		//list options
		txtSchedule = (TextView) findViewById(R.id.schedules_content);
		txtPrice = (TextView) findViewById(R.id.price_content);
		txtLang = (TextView) findViewById(R.id.lang_content);
		txtCapicity = (TextView) findViewById(R.id.capacity_content);
		txtWine = (TextView) findViewById(R.id.wines_content);
		txtVisit = (TextView) findViewById(R.id.visit_content);
		

		init();
	}

	void init() {
		
		if (celler.getCellarName().length() > 30){
			title.setText(celler.getCellarName().substring(0, 27)+"...");
		} else {
			title.setText(celler.getCellarName());
		}
		
		if (celler.getNameCategory().length() > 15){
			titleCategory.setText(celler.getNameCategory().substring(0, 12) + "...");
		} else {
			titleCategory.setText(celler.getNameCategory());
		}
		
		txtDientich.setText(celler.getHect());
		txtSanluong.setText(doubleToStringNoDecimal(Double.parseDouble(celler.getAmpoller())));
		txtDes.setText(celler.getDesCriptionE());
		btnPhone.setText("+34 "+celler.getPhone());
		
		ImageLoader.getInstance().displayImage("http://enoguia.cat/img/logos/"+celler.getLogo(), imgLogo, options);
		ImageLoader.getInstance().displayImage("http://enoguia.cat/img/fotos/"+celler.getPhoto(), imgCover, options);
		
		txtAddress.setText(celler.getAddress());
		
		// options values;
		txtSchedule.setText(celler.getHorariE());
		txtPrice.setText(celler.getPriceE());
		txtLang.setText(celler.getLanguageE());
		txtCapicity.setText("Min: "+celler.getCapityMin()+ " Max: "+celler.getCapityMax());
		txtWine.setText(celler.getVinE());
		txtVisit.setText(celler.getVisitYes());
		
		txtListVins.setText(celler.getListtaVins());
		
		btnBack.setOnTouchListener(this);
		
		btnPhone.setOnTouchListener(this);
		btnWeb.setOnTouchListener(this);
		btnEmail.setOnTouchListener(this);
		btnFace.setOnTouchListener(this);
		btnTw.setOnTouchListener(this);
		
//		titleDirection.setOnTouchListener(this);
		
		llDirection.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.i("DATA", "direction-----------");
				Intent t = new Intent(DetailBussinessActivity.this, DirectionActivity.class);
				Log.i("DATA", "LAT: "+celler.getLat());
				t.putExtra("celler", celler);
				startActivity(t);
			}
		});
		
		btnLike.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/*if (liked == 0){
					db.insertLike(celler, 1);	
					liked = 1;
				} else if (liked == 1) {
					liked = 0;
					db.insertLike(celler, 0);
				} */
				
				if (liked == 0){
					db.insertIntoTableLike(celler, 1);
					liked = 1;
					btnLike.setImageResource(R.drawable.btn_like_selector_dis);
				} else if (liked == 1) {
					liked = 0;
					db.unlike(celler.getId());
					btnLike.setImageResource(R.drawable.btn_like_selector);
				} 
			}
		});
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn_back:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				finish();
			}
			break;
			
		case R.id.btn_phone:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				callPhone();
			}
			break;
		case R.id.btn_web:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				openWeb();
			}
			break;
		case R.id.btn_email:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				composeEmail();
			}
			break;
		case R.id.ll_direction:
			if (event.getAction() == MotionEvent.ACTION_UP){
				
			}
			break;
		case R.id.btn_facebook:
			if (event.getAction() == MotionEvent.ACTION_UP){
				shareUrlFacebook();
			}
			break;
		case R.id.btn_tw:
			if (event.getAction() == MotionEvent.ACTION_UP){
				Intent i = new Intent(Intent.ACTION_SEND);
				i.setType("text/plain");
				i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
				i.putExtra(Intent.EXTRA_TEXT, "http://"+celler.getWeb());
				startActivity(Intent.createChooser(i, "Share URL"));
			}
			break;
		default:
			break;
		}
		
		return false;
	}
	 
	void callPhone(){
		
		dC = new Dialog(this);
		dC.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dC.setContentView(R.layout.dialog_confirm_call_phone);
		
		
		TextView txtThoat = (TextView) dC.findViewById(R.id.str_canhbao_thoat);
		
		Button btnCancel = (Button) dC.findViewById(R.id.btn_cancel);
		Button btnCall = (Button) dC.findViewById(R.id.btn_call);
		dC.show();
		dC.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dC.setCanceledOnTouchOutside(true);
		dC.getWindow().setGravity(Gravity.CENTER);
		dC.show();
		
		// setonclick
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dC.dismiss();
			}
		});
		btnCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dC.dismiss();
				call();
			}
		});
		
	}
	
	public void call() {   
        Intent callIntent = new Intent(Intent.ACTION_CALL);          
        callIntent.setData(Uri.parse("tel:"+celler.getPhone()));          
        startActivity(callIntent);  
	}
	
	void openWeb(){
		dW = new Dialog(this);
		dW.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dW.setContentView(R.layout.dialog_confirm_open_web);
		
		
		TextView txtThoat = (TextView) dW.findViewById(R.id.str_canhbao_thoat);
		
		Button btnCancel = (Button) dW.findViewById(R.id.btn_cancel);
		Button btnCall = (Button) dW.findViewById(R.id.btn_call);
		dW.show();
		dW.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dW.setCanceledOnTouchOutside(true);
		dW.getWindow().setGravity(Gravity.CENTER);
		dW.show();
		
		// setonclick
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dW.dismiss();
			}
		});
		btnCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dW.dismiss();
				web();
			}
		});
	}
	
	public void web(){
		Uri uriUrl = Uri.parse("http://"+celler.getWeb()); 
		Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);  
		startActivity(launchBrowser);
	}
	
	void composeEmail(){
		dM = new Dialog(this);
		dM.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dM.setContentView(R.layout.dialog_confirm_compose_email);
		
		
		TextView txtThoat = (TextView) dM.findViewById(R.id.str_canhbao_thoat);
		
		Button btnCancel = (Button) dM.findViewById(R.id.btn_cancel);
		Button btnCall = (Button) dM.findViewById(R.id.btn_call);
		dM.show();
		dM.getWindow().setBackgroundDrawable(new ColorDrawable(0));
		dM.setCanceledOnTouchOutside(true);
		dM.getWindow().setGravity(Gravity.CENTER);
		dM.show();
		
		// setonclick
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dM.dismiss();
			}
		});
		btnCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dM.dismiss();
				email();
			}
		});
	}
	
	public void email(){
		Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+celler.getEmail()));
		emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email Subject");
		startActivity(Intent.createChooser(emailIntent, "Chooser Title"));
	}
	
	// share link to fb
		public void shareUrlFacebook() {
			/*
			 * Facebook - "com.facebook.katana" Twitter - "com.twitter.android"
			 * Instagram - "com.instagram.android" Pinterest - "com.pinterest"
			 */
			// getPackageName() from Context or Activity object
			final String appPackageName = "com.facebook.katana";
			Intent intent = getPackageManager().getLaunchIntentForPackage(
					"com.facebook.katana");
			
			
			if (intent != null) {
				Intent share = new Intent(android.content.Intent.ACTION_SEND);
				share.setType("text/plain");
				share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
				// add data
				share.putExtra(Intent.EXTRA_TEXT, "http://"+celler.getWeb());
				startActivity(Intent.createChooser(share, "Enoguia"));

			} else {

				try {
					startActivity(new Intent(Intent.ACTION_VIEW,
							Uri.parse("market://details?id=" + appPackageName)));
				} catch (android.content.ActivityNotFoundException anfe) {
					startActivity(new Intent(
							Intent.ACTION_VIEW,
							Uri.parse("http://play.google.com/store/apps/details?id="
									+ appPackageName)));
				}
			}
		}
		
		 public  String doubleToStringNoDecimal(double d) {
		        DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance(Locale.UK);;
		        formatter .applyPattern("#,###");
		        return formatter.format(d);
		    }

	
	
}
